package com.ohuang.wallpapermanager.vm

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohuang.wallpapermanager.compose.update
import com.ohuang.wallpapermanager.config.LocalConfig
import com.ohuang.wallpapermanager.repository.WallPaperInfoRepository
import com.ohuang.wallpapermanager.screen.SettingScreenState
import com.ohuang.wallpapermanager.screen.SettingScreenStateEvent
import com.ohuang.wallpapermanager.screen.SettingScreenWallPaperState
import com.ohuang.wallpapermanager.util.TimeUtil
import com.ohuang.wallpapermanager.wallpaper.CarouselWallPaperDispatch
import com.ohuang.wallpapermanager.workmanager.CarouseWallPaperWorkManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SettingViewModel : ViewModel() {


    private val _settingScreenState =
        mutableStateOf(SettingScreenState(isLoading = true, categoryEnable = false))
    val settingScreenState: State<SettingScreenState> = _settingScreenState
    private var lockRepository = WallPaperInfoRepository(isLock = true)
    private var homeRepository = WallPaperInfoRepository(isLock = false)

    suspend fun init() {
        withContext(Dispatchers.IO) {
            refresh()
        }
    }

    private fun refresh() {
        _settingScreenState.update {
            it.copy(
                isUseWallPaperService = LocalConfig.isUseWallPaperService,
                categoryEnable = LocalConfig.isWorkManagerEnable,
                wallPaperServiceShowMode = LocalConfig.wallPaperServiceShowMode,
                lastUpdateTime = TimeUtil.getTime(LocalConfig.workManagerLastRunTime),
                lockWallPaper = getSettingScreenWallPaperState(true),
                homeWallPaper = getSettingScreenWallPaperState(false), isLoading = false
            )
        }
    }

    private fun getSettingScreenWallPaperState(isLock: Boolean): SettingScreenWallPaperState {
        val wallPaperInfoRepository = if (isLock) {
            lockRepository
        } else {
            homeRepository
        }

        val db = wallPaperInfoRepository.getConfig()
        val msg = if (db.minRandomInterval <= 60_000L * 15) {
            "15分钟"
        } else if (db.minRandomInterval <= 60_000L * 30) {
            "30分钟"
        } else {
            "${db.minRandomInterval / (3600_000L)}小时"
        }

        return SettingScreenWallPaperState(
            categoryEnable = db.isCarouselEnable,
            randomInterval = msg,
            lastUpdateTime = TimeUtil.getTime(db.lastUpdateTime)
        )
    }


    fun onEvent(it: SettingScreenStateEvent) {
        when (it) {
            is SettingScreenStateEvent.SynchronizationWallWallPaper -> {
                synchronizationWallWallPaper(it.isForced)
            }

            is SettingScreenStateEvent.CategoryEnable -> {
                LocalConfig.isWorkManagerEnable = it.enable
                CarouseWallPaperWorkManager.start(false)
                refresh()
            }

            is SettingScreenStateEvent.WallPaperRandomInterval -> {
                wallPaperRandomInterval(isLock = it.isLock, interval = it.interval)
            }

            is SettingScreenStateEvent.WallPaperCategoryEnable -> {
                wallPaperCategoryEnable(isLock = it.isLock, enable = it.enable)
            }

            is SettingScreenStateEvent.UseWallPaperService -> {
                LocalConfig.isUseWallPaperService = it.isUse
                refresh()
            }
            is SettingScreenStateEvent.WallPaperServiceShowMode->{
                LocalConfig.wallPaperServiceShowMode = it.mode
                refresh()
            }
        }
    }

    private fun wallPaperCategoryEnable(isLock: Boolean, enable: Boolean) {
        val wallPaperInfoRepository = if (isLock) {
            lockRepository
        } else {
            homeRepository
        }
        wallPaperInfoRepository.getConfig().isCarouselEnable = enable
        refresh()
    }

    private fun wallPaperRandomInterval(isLock: Boolean, interval: Long) {
        val wallPaperInfoRepository = if (isLock) {
            lockRepository
        } else {
            homeRepository
        }
        wallPaperInfoRepository.getConfig().minRandomInterval = interval
        refresh()
    }

    private fun synchronizationWallWallPaper(boolean: Boolean) {
        viewModelScope.launch(Dispatchers.IO) {
            _settingScreenState.update { it.copy(isLoading = true) }
            if (boolean) {
                lockRepository.clearSettingConfig()
                homeRepository.clearSettingConfig()
            }
            CarouselWallPaperDispatch.changeWallPaper()
            _settingScreenState.update { it.copy(isLoading = false) }
        }

    }

}